package main;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import dao.AccessesDAO;
import dao.AccessesDAOImpl;

public class ReportDriver {

	private static String loc = "resources//logs_02_1926011.tar.gz";
	private static String loc2 = "resources//logs_03_12_2011.tar.gz";
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		reportCountryCityAlg();
		reportAlgorithmCountryCity();
		reportCountryCityUser();
		
	}
	
	public static void reportCountryCityAlg(){
		AccessesDAO dao = new AccessesDAOImpl();
		File report1 = new File("logreports/CountryCityAlg_Report.txt");
		try {
			FileWriter fw = new FileWriter(report1);
		
			List<DataTransferObject> dtos = dao.getCountryCityAlgorithm();
			String format = "%1$-20s%2$-20s%3$-40s%4$-40s%5$-20s";
			String out = String.format(format,
					"Country", "City", "Algorithm Name",
					"Username", "Time");
			System.out.println(out);
			String line = "--------------------------------------------------------------------------------------------------------------------------------------------------------------";
			System.out.println(line);
			fw.write(out + "\n");
			fw.write(line+ "\n");
			
			for(DataTransferObject dto : dtos){
				String calString = String.format("%1$tm/%1$te/%1$tY %1$tH:%1$tM", dto.getTime());
				out = String.format(format,
						dto.getCountry(), dto.getCity(), dto.getAlgorithmName(),
						dto.getUserName(), calString);
				System.out.println(out);
				fw.write(out + "\n");
			}
			
			fw.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
		
	public static void reportCountryCityUser(){
		AccessesDAO dao = new AccessesDAOImpl();
		File report1 = new File("logreports/CountryCityUser_Report.txt");
		try {
			FileWriter fw = new FileWriter(report1);
		
			List<DataTransferObject> dtos = dao.getCountryCityUser();
			String format = "%1$-20s%2$-20s%3$-40s%4$-40s%5$-20s";
			String out = String.format(format,
					"Country", "City", "User Name",
					"Algorithm Name", "Time");
			System.out.println(out);
			String line = "--------------------------------------------------------------------------------------------------------------------------------------------------------------";
			System.out.println(line);
			fw.write(out + "\n");
			fw.write(line+ "\n");
			
			for(DataTransferObject dto : dtos){
				String calString = String.format("%1$tm/%1$te/%1$tY %1$tH:%1$tM", dto.getTime());
				out = String.format(format,
						dto.getCountry(), dto.getCity(), dto.getUserName(),
						dto.getAlgorithmName(), calString);
				System.out.println(out);
				fw.write(out + "\n");
			}
			
			fw.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void reportAlgorithmCountryCity(){
		AccessesDAO dao = new AccessesDAOImpl();
		File report1 = new File("logreports/AlgCountryCity_Report.txt");
		try {
			FileWriter fw = new FileWriter(report1);
		
			List<DataTransferObject> dtos = dao.getAlgorithmCountryCity();
			String format = "%1$-20s%2$-20s%3$-40s%4$-40s%5$-20s";
			String out = String.format(format,
					"Algorithm Name", "Country", "City",
					"Username", "Time");
			System.out.println(out);
			String line = "--------------------------------------------------------------------------------------------------------------------------------------------------------------";
			System.out.println(line);
			fw.write(out + "\n");
			fw.write(line+ "\n");
			
			for(DataTransferObject dto : dtos){
				String calString = String.format("%1$tm/%1$te/%1$tY %1$tH:%1$tM", dto.getTime());
				out = String.format(format,
						dto.getAlgorithmName(), dto.getCountry(), dto.getCity(),
						dto.getUserName(), calString);
				System.out.println(out);
				fw.write(out + "\n");
			}
			
			fw.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
